package com.yinhai.message.implement.app.util;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import com.yinhai.core.common.api.util.ValidateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;


import com.yinhai.core.app.api.util.ServiceLocator;
import com.yinhai.message.api.bean.TMMessage;
import com.yinhai.message.api.bean.TMUser;
import com.yinhai.message.api.vo.TMMessageVo;
import com.yinhai.message.core.util.IMessageConstant;
import com.yinhai.message.manage.register.bpo.IAppConfigBpo;
import com.yinhai.message.manage.register.vo.AppConfigVo;

/**
 * @author:TANQINGPING
 * @version:1.0 2017/3/31
 * package:com.yinhai.message.implement.app
 * <p>Title: AppPushUtil.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: 四川久远银海软件股份有限公司</p>
 */
public class AppPushUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(AppPushUtil.class);


    public static List<TMUser> pushMessage(TMMessageVo appMessageVo) {
        List<TMUser> receiveUsers = appMessageVo.getUserlist();
        List<TMUser> resultUserList = new ArrayList<>();
        TMMessage message = appMessageVo.getMessage();
        IAppConfigBpo appConfigBpo = (IAppConfigBpo) ServiceLocator.getService("appConfigBpo");
        //发送所有人
        if(message.isToAlluser()){
            TMUser user = appMessageVo.getUser();
            TMUser exuser = new TMUser();
            exuser.setAppId(user.getAppId());
            exuser.setUserId(user.getUserId());
            exuser.setSendSign(user.getSendSign());
            AppConfigVo configVo = appConfigBpo.queryAppBySystemId(user.getAppId());
            try {
                PushContontrol.pushMessage(null,message,configVo.getMasterSecret(), configVo.getAppKey());
                exuser.setSendSign("1");
            } catch (Exception e) {
                e.printStackTrace();
                exuser.setSendSign(IMessageConstant.MSG_STATE_SEND_FAIL);
                exuser.setErrorInfo(e.getMessage());
            }
            resultUserList.add(exuser);
            //未发送所有人
        }else{
            for (TMUser user : receiveUsers) {
                if(ValidateUtil.isEmpty(user.getRegistrationId())){
                    continue;
                }
                TMUser exuser = new TMUser();
                exuser.setAppId(user.getAppId());
                exuser.setUserId(user.getUserId());
                exuser.setSendSign(user.getSendSign());
                exuser.setRegistrationId(user.getRegistrationId());
                AppConfigVo configVo = appConfigBpo.queryAppBySystemId(user.getAppId());
                if (configVo != null && !StringUtils.isEmpty(configVo.getAppKey()) && !StringUtils.isEmpty(configVo.getMasterSecret())) {
                    //设置当前设备在生产环境中 (也就是正式环境、上线环境)
                    try {
                        PushContontrol.pushMessage(exuser,message,configVo.getMasterSecret(), configVo.getAppKey());
                        exuser.setSendSign("1");
                    } catch (Exception e) {
                        e.printStackTrace();
                        exuser.setSendSign(IMessageConstant.MSG_STATE_SEND_FAIL);
                        exuser.setErrorInfo(e.getMessage());
                    }
                } else {
                    LOGGER.error("no result data or the fields APPKEY/MASTERSECRET is NULL ");
                    exuser.setSendSign("2");
                    exuser.setErrorInfo("no result data or the fields APPKEY/MASTERSECRET is NULL");
                }
                resultUserList.add(exuser);
            }
        }
        return resultUserList;
    }
}
